--use PolePosition
GO

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'up_UtilityAssetDistribution_SelectForExcel')
	DROP PROCEDURE up_UtilityAssetDistribution_SelectForExcel
GO

CREATE PROCEDURE up_UtilityAssetDistribution_SelectForExcel
	 @UtilityFk int 
	,@SearchByType varchar(50) = null
	,@SearchByText varchar(50) = null
AS

SET NOCOUNT ON	


SELECT 
 	 Pole.[Utility_Asset_DistributionId] AS [Asset]
	,Maps.[Name] AS [Map]
	,SectionFk AS [Section]
	,dbo.fn_DistributionPole_GetPhaseString(Utility_Asset_DistributionId, 1) AS [Phase]
	,dbo.fn_DistributionPole_GetTopTypeString(Utility_Asset_DistributionId, 1) AS [Top Type]
	,Species.[Name] AS [Species]
	,Class.[Name] AS [Class]
	,Pole.[Length] AS [Length]
	,Pole.[Year] AS [Year]
	,Treatment.[Name] AS [Treatment]
	,Pole.[Longitude] AS [Longitude]	
	,Pole.[Latitude] AS [Latitude]
	,Pole.[Number] AS [Number]
	,NULL AS [Last Visit]
FROM v_tb_Utility_Asset_Distributions Pole
LEFT JOIN tb_XMissionStructures_PoleSpecies Species
	ON Species.XMissionStructures_PoleSpeciesId = Pole.SpeciesFk
LEFT JOIN tb_XMissionStructures_PoleTreatments Treatment
	ON Treatment.XMissionStructures_PoleTreatmentId = Pole.TreatmentFk
LEFT JOIN tb_XMissionStructures_PoleClasses Class
	ON Class.XMissionStructures_PoleClassId = Pole.ClassFk
LEFT JOIN tb_Utility_Polygons Maps
	ON Maps.Utility_PolygonId = Pole.[MapFk]
WHERE
	Pole.[UtilityFK] = @UtilityFk

GO

